package GoodsService

import (
	toolsDb "gdshop-back-end-go/common/db"
	"gdshop-back-end-go/common/request/BaseReq"
	"gdshop-back-end-go/common/response"
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"strings"
)

func GetLimitRules(r *ghttp.Request, req *BaseReq.I) *response.JsonResponse {
	ctx := r.GetCtx()
	v, err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "goods_limit").Where("id", req.Id).Value("limit_rules")
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}

	content, err := gjson.LoadContent(v.String())
	if err != nil {
		return response.FailByRequestMessage(nil, err.Error())
	}

	tmps := content.GetStrings(".")

	fanhui := g.List{}
	for _, tmp := range tmps {
		if tmp == "" {
			continue
		}
		t := strings.Split(tmp, "")
		switch len(tmp) {
		case 9:
			fanhui = append(fanhui, g.Map{
				"code_text": tmp,
				"code_array": []string{
					t[0] + t[1],
					t[2] + t[3],
					t[4] + t[5],
					t[6] + t[7] + t[8],
				},
			})
			break
		case 6:
			fanhui = append(fanhui, g.Map{
				"code_text": tmp,
				"code_array": []string{
					t[0] + t[1],
					t[2] + t[3],
					t[4] + t[5],
				},
			})
			break
		case 4:
			fanhui = append(fanhui, g.Map{
				"code_text": tmp,
				"code_array": []string{
					t[0] + t[1],
					t[2] + t[3],
				},
			})
			break
		case 2:
			fanhui = append(fanhui, g.Map{
				"code_text": tmp,
				"code_array": []string{
					t[0] + t[1],
				},
			})
			break
		}
	}

	return response.SuccessByRequestMessageData(nil, "成功", fanhui)
}
